package com.ymatou.iapi.optrade.testcase;

import java.math.BigDecimal;
import java.util.Arrays;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.optrade.parameter.req.ChangeEcommerceFlagReq;
import com.ymatou.iapi.optrade.parameter.req.PlaceOrderReq;
import com.ymatou.iapi.optrade.parameter.vo.OrderGroupVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderItemVo;
import com.ymatou.iapi.optrade.parameter.vo.OrderVo;
import com.ymatou.iapi.optrade.resp.TradingServiceResp;
import com.ymatou.iapi.optrade.service.ChangeEcommerceFlagCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.YMTTrading.TradingCallService;
import com.ymttest.business.service.YMTTrading.TradingProductVo;
import com.ymttest.database.sharding.db.sharding.model.OrderPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderShardingWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

/**
 * 贝海实际电商件通知接口
 */
@TestSuite
public class Ts_ChangeEcommerceFlag {
	
	private static ChangeEcommerceFlagReq changeEcommerceFlagReq;
	private static ChangeEcommerceFlagCall changeEcommerceFlagCall;
	
	private static OrderShardingWapper database = new OrderShardingWapper();
	
	private static Long sellerId = Long.valueOf(EnvSetup.getData("autotest_seller1"));
	private static String sellerName = String.valueOf(EnvSetup.getData("autotest_sellerName1"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("贝海实际电商件通知接口");
	}

	@Before
	public void caseUp() {
		changeEcommerceFlagReq = new ChangeEcommerceFlagReq();
		changeEcommerceFlagCall = new ChangeEcommerceFlagCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(changeEcommerceFlagCall.getOpurl());
		
		database.activeDeadCommunicationsLink(database);
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 下单
	 * @param receiveName
	 * @param isEcommerceFlag
	 * @return
	 */
	public static TradingServiceResp placeOrder(PlaceOrderReq placeOrderReq,
			boolean isEcommerceFlag) {

		OrderVo orderVo = placeOrderReq.getOrderGroups().get(0).getOrders()
				.get(0);

		orderVo.setEcommerceFlag(isEcommerceFlag);

		return TradingCallService.callPlaceOrderService(placeOrderReq);

	}

	private void verifyDb() {

		long orderId = changeEcommerceFlagReq.getOrderId();

		// 订单表(order)
		OrderPo ymtOrders = database.selectOrderbyOrderId(orderId);

		Logger.comment(String.format("验证订单表(order)表 orderId: %s", orderId));
		Logger.verifyEquals(changeEcommerceFlagReq.getEcommerceFlag(),
				ymtOrders.getActualEcommerce(), "验证actualEcommerce");

	}

	/**
	 * 依赖 身份证查询站  idcardquery.iapi.ymatou.com
	 */

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeEcommerceFlag_001() {
		Logger.start(true, "订单预测个人件,贝海通知为电商件");
		try {
			Logger.comment("前置数据准备");
			PlaceOrderReq placeOrderReq = TradingCallService
					.buildStandPlaceOrderReq();
			
			OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
			OrderVo orderVo = TradingCallService.createOrderVo();
			
			// 创建一个普通商品
			TradingProductVo product = TradingProductVo.createPro(sellerId,
					sellerName, 100);
			
			OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
					product.getProductId(), product.getCatalogId());
			
			orderVo.setOrderItems(Arrays.asList(orderItemVo));
			orderGroupVo.setOrders(Arrays.asList(orderVo));
			placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));

			placeOrderReq.setRecvName(TestDataManager.getChineseName());
			TradingServiceResp placeOrderResp = placeOrder(placeOrderReq, false);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, useAccountAmount, tradingId);

			Logger.comment("前置数据准备结束");
			changeEcommerceFlagReq.setOrderId(orderId);
			changeEcommerceFlagReq.setEcommerceFlag(1);
			changeEcommerceFlagReq.setAppId("autotest");
			changeEcommerceFlagReq.setBillCode("autotest111");

			changeEcommerceFlagCall.setData(changeEcommerceFlagReq);
			changeEcommerceFlagCall.callService();

			Logger.verifyEquals("ok", changeEcommerceFlagCall.getReturnData(),
					"验证接口返回报文");

			//数据库验证
			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeEcommerceFlag_002() {
		Logger.start(true, "订单预测个电商件,贝海通知为个人件");
		try {
			Logger.comment("前置数据准备");
			PlaceOrderReq placeOrderReq = TradingCallService
					.buildStandPlaceOrderReq();
			
			OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
			OrderVo orderVo = TradingCallService.createOrderVo();
			
			// 创建一个普通商品
			TradingProductVo product = TradingProductVo.createPro(sellerId,
					sellerName, 100);
			
			OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
					product.getProductId(), product.getCatalogId());
			
			orderVo.setOrderItems(Arrays.asList(orderItemVo));
			orderGroupVo.setOrders(Arrays.asList(orderVo));
			placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));

			placeOrderReq.setRecvName(TestDataManager.getChineseName());
			TradingServiceResp placeOrderResp = placeOrder(placeOrderReq, true);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, useAccountAmount, tradingId);

			Logger.comment("前置数据准备结束");
			changeEcommerceFlagReq.setOrderId(orderId);
			changeEcommerceFlagReq.setEcommerceFlag(0);
			changeEcommerceFlagReq.setAppId("autotest");
			changeEcommerceFlagReq.setBillCode("autotest111");

			changeEcommerceFlagCall.setData(changeEcommerceFlagReq);
			changeEcommerceFlagCall.callService();

			Logger.verifyEquals("ok", changeEcommerceFlagCall.getReturnData(),
					"验证接口返回报文");

			//数据库验证
			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeEcommerceFlag_003() {
		Logger.start(true, "订单预测个电商件,贝海通知为不存在的类型件3");
		try {
			Logger.comment("前置数据准备");

			PlaceOrderReq placeOrderReq = TradingCallService
					.buildStandPlaceOrderReq();
			
			OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
			OrderVo orderVo = TradingCallService.createOrderVo();
			
			// 创建一个普通商品
			TradingProductVo product = TradingProductVo.createPro(sellerId,
					sellerName, 100);
			
			OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
					product.getProductId(), product.getCatalogId());
			
			orderVo.setOrderItems(Arrays.asList(orderItemVo));
			orderGroupVo.setOrders(Arrays.asList(orderVo));
			placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));

			placeOrderReq.setRecvName(TestDataManager.getChineseName());
			TradingServiceResp placeOrderResp = placeOrder(placeOrderReq, true);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, useAccountAmount, tradingId);

			Logger.comment("前置数据准备结束");
			changeEcommerceFlagReq.setOrderId(orderId);
			changeEcommerceFlagReq.setEcommerceFlag(3);
			changeEcommerceFlagReq.setAppId("autotest");
			changeEcommerceFlagReq.setBillCode("autotest111");

			changeEcommerceFlagCall.setData(changeEcommerceFlagReq);
			changeEcommerceFlagCall.callService();

			changeEcommerceFlagReq.setEcommerceFlag(0);

			Logger.verifyEquals("ok", changeEcommerceFlagCall.getReturnData(),
					"验证接口返回报文");

			//数据库验证

			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	//order_accepted_no_dispatch_notify 消息
	public void Tc_ChangeEcommerceFlag_004() {
		Logger.start(true, "订单预测个人件,贝海通知为个人件 用户没上传过身份证号,推送消息提醒");
		try {
			Logger.comment("前置数据准备");

			PlaceOrderReq placeOrderReq = TradingCallService
					.buildStandPlaceOrderReq();
			
			OrderGroupVo orderGroupVo = TradingCallService.createOrderGroupVo(sellerId, sellerName);
			OrderVo orderVo = TradingCallService.createOrderVo();
			
			// 创建一个普通商品
			TradingProductVo product = TradingProductVo.createPro(sellerId,
					sellerName, 100);
			
			OrderItemVo orderItemVo = TradingCallService.createOrderItemVo(
					product.getProductId(), product.getCatalogId());
			
			orderVo.setOrderItems(Arrays.asList(orderItemVo));
			orderGroupVo.setOrders(Arrays.asList(orderVo));
			placeOrderReq.setOrderGroups(Arrays.asList(orderGroupVo));

			placeOrderReq.setRecvName(TestDataManager.getChineseName());

			TradingServiceResp placeOrderResp = placeOrder(placeOrderReq, true);

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			long userId = placeOrderResp.getUserId();
			long tradingId = placeOrderResp.getTradingId();
			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			BigDecimal useAccountAmount = placeOrderResp.getPayableAmount();

			TradingCallService.payOrder(userId, useAccountAmount, tradingId);

			//接单
			TradingCallService.sellerAcceptOrder(sellerId, orderId);

			Logger.comment("前置数据准备结束");
			changeEcommerceFlagReq.setOrderId(orderId);
			changeEcommerceFlagReq.setEcommerceFlag(0);
			changeEcommerceFlagReq.setAppId("autotest");
			changeEcommerceFlagReq.setBillCode("autotest111");

			changeEcommerceFlagCall.setData(changeEcommerceFlagReq);
			changeEcommerceFlagCall.callService();

			Logger.verifyEquals("ok", changeEcommerceFlagCall.getReturnData(),
					"验证接口返回报文");

			//数据库验证
			verifyDb();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
